<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="phone">
          <el-input
            v-model="loginForm.phone"
            prefix-icon="iconfont icon-user"
            maxlength="11"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="userPwd">
          <el-input
            v-model="loginForm.userPwd"
            prefix-icon="iconfont icon-3702mima"
            type="userPwd"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login" :loading="loading"
            >登录</el-button
          >
          <el-button type="info" @click="resetLoginForm">验证码登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login, getShopInfo } from "@/api/user.js";
export default {
  data() {
    return {
      loading: false,
      loginForm: {
        phone: "",
        userPwd: "",
      },
      loginFormRules: {
        // 验证手机号是否合法
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            min: 11,
            max: 11,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
          {
            pattern: /^1[3456789]\d{9}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
        // 验证密码是否合法
        userPwd: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    resetLoginForm() {
      this.$router.push("/scanTheCode");
    },
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return;
        const jsonObject = {
          ...this.loginForm,
        };
        this.loading = true;
        login(jsonObject)
          .then((res) => {
            console.log(res);
            if (res.code !== "200") {
              this.$message.error("登录失败：" + res.msg);
              return;
            } else {
              localStorage.setItem("kua_token", res.data.sessionToken);
              getShopInfo().then((res) => {
                if (!res.data) {
                  this.$message.error("请下载夸奖君APP开店，夸小店");
                  return;
                } else {
                  if (res.data.shop.status == 1) {
                    localStorage.setItem("shop", JSON.stringify(res.data.shop));
                    this.$message.success("登录成功");
                    this.$router.push("/classify");
                    return;
                  } else if (res.data.shop.status == 0) {
                    this.$message.error("您的店铺正在审核中~");
                    return;
                  } else {
                    this.$message.error("您的店铺审核未通过~");
                    return;
                  }
                }
              }).catch((err) => {
        console.log(err);
      })
            }
          })
          .finally(() => {
            this.loading = false;
          });
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>